package com.u.iot.sleep.config;

import org.eclipse.paho.mqttv5.client.MqttClient;
import org.eclipse.paho.mqttv5.client.MqttConnectionOptions;
import org.eclipse.paho.mqttv5.common.MqttException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @program: u-InternetOfThings
 * @description: mqtt客户端配置
 * @author: Alex Wu
 * @createDate: 2025-03-19 17:20
 **/
@Configuration
public class MqttClientConfig {

    @Value("${mqtt.broker}")
    private String mqttBroker;

    @Value("${mqtt.client-id}")
    private String clientId;

    @Value("${mqtt.username}")
    private String username;

    @Value("${mqtt.password}")
    private String password;

//    @Bean
    public MqttClient mqttClient() throws MqttException {
        MqttClient client = new MqttClient(mqttBroker, clientId);
        MqttConnectionOptions options = new MqttConnectionOptions();
//        options.setUserName(USERNAME);
//        options.setPassword(PASSWORD.getBytes());
        options.setCleanStart(true);
        options.setAutomaticReconnect(true);
        client.connect(options);
        return client;
    }
}
